#!/usr/bin/env bash

source "$(dirname "$0")/common.sh"
check_requirements

#-------------------------------------------------------------------------------
function usage() {
   echo "Make a Kong patch release using this script:"
   echo ""
   echo "Usage:"
   if [ "$version" = "<x.y.z>" ]
   then
      echo "     List executed steps for a given release"
      echo "        $0 $version $1 $3"
      echo
   fi
   c=1
   step "check_milestone"      "ensure all PRs marked on the release milestone are 100% merged"
   step "check_dependencies"   "ensure all kong dependencies are bumped in the rockspec"
   step "check_perf"           "ensure performance tests were executed"
   step "check_upgrade_tests"  "ensure upgrade tests were executed"
   step "check_changelog"      "ensure changelog was written in pre-release and is ready for final"
   step "check_upgrade"        "ensure upgrade.md was updated in pre-release and is ready for final"
   step "version_bump"         "bump and commit the version number"
   step "submit_release_pr"    "push and submit a release PR"
   step "merge"                "merge, tag and sign the release"
   step "approve_docker"       "humans review and approve machine PR to docker-kong repo"
   step "merge_docker"         "merge, tag and sign Kong's docker-kong PR"
   step "submit_docker"        "submit a PR to docker-library/official-images"
   step "merge_homebrew"       "humans approve and merge machine PR to homebrew-kong"
   step "upload_rock"          "upload to LuaRocks" "<api-key>"
   step "merge_vagrant"        "humans approve and merge machine PR to kong-vagrant"
   step "merge_pongo"          "humans approve and merge machine PR to kong-pongo"
   step "announce"             "Get announcement messages for Kong Nation and Slack #general"
   exit 0
}


#-------------------------------------------------------------------------------
# Default help
#-------------------------------------------------------------------------------

if [ "$1" = "-h" ] || [ "$1" = "--help" ] || ! [ "$1" ]
then
   version="<x.y.z>"
   usage "$@"
fi

#-------------------------------------------------------------------------------
# Variables
#-------------------------------------------------------------------------------

version="$1"
step="$2"

major=${version%%.*}
rest=${version#*.}
minor=${rest%%.*}
patch=${rest##*.}
rockspec="kong-$version-0.rockspec"
branch="release/$version"
base="release/$major.$minor.x"

if ! [[ "$version" =~ ^[0-9]+.[0-9]+.[0-9]$ ]]
then
   die "first argument must be a version in x.y.z format"
fi

if [ "$step" = "" ]
then
   usage "$@"
fi

EDITOR="${EDITOR-$VISUAL}"

case "$step" in
   check_milestone) check_milestone ;;
   check_dependencies) check_dependencies ;;

   #---------------------------------------------------------------------------
   check_perf)
      CONFIRM "Ensure Kong performance tests were performed. At minimum that requires running the https://github.com/Kong/kong/actions/workflows/perf.yml on the release branch and on the previous release, and compare the results. It can involve more custom tests. If everything looks all right, press Enter to continue or Ctrl+C to quit"
      SUCCESS "Proceeding."
      ;;

   #---------------------------------------------------------------------------
   check_upgrade_tests)
      CONFIRM "Ensure Kong upgrade tests were performed. Current upgrade tests are https://github.com/Kong/kong-upgrade-tests. There should be tests on every release that has migrations, and no stoppers should appear. If everything looks all right, press Enter to continue or Ctrl+C to quit"
      SUCCESS "Proceeding."
      ;;

   #---------------------------------------------------------------------------
   check_changelog)
      echo "Ensure changelog contains all the changes needed the final. Press 'y' to open the CHANGELOG or Ctrl+C to quit"
      read
      if [ "$REPLY" = "y" ]
      then
        $EDITOR CHANGELOG.md
      fi

      CONFIRM "If everything looks all right, press Enter to continue"
      SUCCESS "CHANGELOG is ready. Proceeding!"
      ;;

   #---------------------------------------------------------------------------
   check_upgrade)
      echo "Ensure UPGRADE.md was written in pre-release and is ready for the final. Press 'y' to open UPGRADE.md or Ctrl+C to quit"
      read
      if [ "$REPLY" = "y" ]
      then
        $EDITOR UPGRADE.md
      fi

      CONFIRM "If everything looks all right, press Enter to continue"
      SUCCESS "UPGRADE.md is ready. Proceeding!"
      ;;

   #---------------------------------------------------------------------------
   version_bump)
      sed -i.bak 's/major = [0-9]*/major = '$major'/' kong/meta.lua
      sed -i.bak 's/minor = [0-9]*/minor = '$minor'/' kong/meta.lua
      sed -i.bak 's/patch = [0-9]*/patch = '$patch'/' kong/meta.lua

      if ! grep -q "\-\-suffix" kong/meta.lua; then
        sed -i.bak 's/suffix =/--suffix = /' kong/meta.lua
      fi

      git add kong/meta.lua

      if ! [ -f "$rockspec" ]
      then
         git mv kong-*-0.rockspec "$rockspec"
         sed -i.bak 's/^version = ".*"/version = "'$version'-0"/' "$rockspec"
         sed -i.bak 's/^  tag = ".*"/  tag = "'$version'"/' "$rockspec"
      fi

      git status
      git diff

      CONFIRM "If everything looks all right, press Enter to make the release commit" \
              "or Ctrl-C to cancel."

      git add $rockspec

      git commit -m "release: $version"
      git log -n 1

      SUCCESS "Version bump for the release is now committed locally." \
              "You are ready to run the next step:" \
              "    $0 $version submit_release_pr"
      ;;

   #---------------------------------------------------------------------------
   submit_release_pr) submit_release_pr "$base" "$version" merge docs_pr;;

   #---------------------------------------------------------------------------
   merge)
      CONFIRM "Press Enter to merge the PR into master and push the tag and Github release" \
              "or Ctrl-C to cancel."

      set -e
      git checkout "$base"
      git pull
      git tag -s "$version" -m "$version"
      git push origin "$version"

      make_github_release_file

      hub release create -F release-$version.txt "$version"
      rm -f release-$version.txt

      git checkout master
      git pull
      git merge "$base"
      git push

      SUCCESS "Make sure the packages are built and available on download.konghq.com" \
              "before continuing to the following steps." \

              "They should be visible on https://internal.builds.konghq.com/job/kong/view/tags/. " \
              "An recurrent task checks for new releases every 15 minutes on the server. " \
              "If needed, the link 'Scan Multibranch Pipeline Now' will scan on-demmand. It can be used " \
              "to attempt to rebuild, if there was an error."

              "As the packages are built, you may run the following steps in parallel:" \
              "* 'upload_luarock'" \
              "* 'merge_homebrew'" \
              "* 'merge_vagrant'" \
              "* 'merge_pongo'" \
              "* 'approve_docker', then 'merge_docker', then 'submit_docker'"
      ;;
   #---------------------------------------------------------------------------
   approve_docker) approve_docker ;;
   merge_docker) merge_docker "$branch" "$version" ;;
   submit_docker) submit_docker "$version";;
   merge_homebrew) merge_homebrew ;;
   merge_pongo) merge_pongo ;;
   merge_vagrant) merge_vagrant ;;
   upload_luarock) upload_luarock "$rockspec" "$3" ;;
   announce) announce "$major" "$minor" "$patch" ;;
   *)
      die "Unknown step!"
      ;;
esac
